import {
  getClientList,

} from '../../../network/client'
import {
  sendUserLogin
} from '../../../network/other';
import {
  toast
} from '../../../utils/Tools'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 弹框显示状态
    // isCause: false,
    flag: true,
    active: 0,
    // 显示队列
    columns: [],
    index: 0,
    // 请求回来的列表数据
    clientList: [],
    // 订单类型
    orderType: "",
    // 页码
    page: 1,
    // 只调用一次函数
    funOpen: true,
    // 三个时间
    arrDate:{}
  },
  onShow() {
    this.userCut()
    if (this.data.funOpen) {
      this._getClientList(0)
    }
  },
  // 改变账号值
  async onChange(event) {
    const accounts = wx.getStorageSync('account')
    // 获取对应账号的密码
    let pwds = ""
    for (let i in accounts) {
      if (accounts[i].user == event.detail.value) {
        pwds = accounts[i].password
      }
    }
    //  event组件自带的
    console.log("数值改变了", event);
    let data = {
      account: event.detail.value,
      pwd: pwds
    }
    const res = await sendUserLogin(data)
    if (res.code === 200) {
      wx.setStorageSync('token', res.data.jwt_token)
      toast("切换账号为" + event.detail.value, 'success')
    }
    console.log("res", res);
  },
  userCut() {
    const value = wx.getStorageSync('account')

    let columnsList = []
    for (let i in value) {
      columnsList.push(value[i].user)
      this.setData({
        columns: columnsList
      })
    }
  },

  // 导航栏切换
  handleTabActive(e) {
    console.log("激活", this.data.active);
    const {
      index
    } = e.detail

    this.setData({
      active: index,
      page: 1,
      clientList: ''
    })

    this._getClientList(index)


    console.log("clientList", this.data.clientList);
  },
  /**
   * 页面触底事件的处理函数
   */
  onReachBottom: function () {
    //当页面触底时,将页码+1
    this.setData({
      page: this.data.page + 1
    })
    //重新调用数据请求函数
    setTimeout(() => {
      this._getClientList(this.data.active)
    }, 1000);

    console.log("页面触底", this.data.active);
  },
  //  一大串判断的
  async _getClientList(index) {
    console.log("page", this.data.page);
    this.setData({
      funOpen: false
    })
    // 待审核
    if (index === 0) {
      var dataList = {
        code: "0",
        status: "0",
        preget: "0",
        order_type: "order_find",
        order_status: "0",
        page: this.data.page
      }
      this.setData({
        orderType: 'order_find'
      })
      // 待分配
    } else if (index === 1) {
      var dataList = {
        code: "1",
        status: "1",
        preget: "0",
        order_type: "order_find",
        order_status: "0",
        page: this.data.page
      }
      this.setData({
        orderType: 'order_find'
      })
      // 待接单
    } else if (index === 2) {
      var dataList = {
        code: "1",
        status: "1",
        preget: "0",
        order_type: "base_table_find",
        // base_table_find
        order_status: "0",
        page: this.data.page
      }
      this.setData({
        orderType: 'base_table_find'
      })
      // 待提货
    } else if (index === 3) {
      var dataList = {
        code: "1",
        status: "1",
        preget: "1",
        driver_status: "0",
        order_type: "base_table_find",
        order_status: "0",
        page: this.data.page
      }
      this.setData({
        orderType: 'base_table_find'
      })

    } else if (index === 4) {
      // 进行中
      var dataList = {
        code: "1",
        status: "1",
        preget: "1",
        driver_status: "1",
        order_status: "0",
        order_type: "base_table_find",

      }
      this.setData({
        orderType: 'base_table_find'
      })
      // 已完成
    } else if (index === 5) {
      var dataList = {
        code: "3",
        status: "1",
        preget: "1",
        driver_status: "2",
        order_type: "base_table_find",
        page: this.data.page

      }
      this.setData({
        orderType: 'base_table_find'
      })
      // 未通过
    } else if (index === 6) {
      var dataList = {
        code: "2",
        status: "1",
        preget: "0",
        order_type: "order_find",
        page: this.data.page
      }
      this.setData({
        orderType: 'order_find'
      })
      // 取消订单
    } else if (index === 7) {
      var dataList = {
        code: "1",
        status: "1",
        preget: "0",
        order_status: "-1",
        order_type: "order_find",
        page: this.data.page
      }
      this.setData({
        orderType: 'order_find'
      })
    }

    const data = dataList
    const res = await getClientList(data)
    if (res.code === 200) {
      if (this.data.clientList) {
        var tableTem = this.data.clientList
        this.setData({
          clientList: tableTem.concat(res.data.order.data)
        })
      } else {
        this.setData({
          clientList: res.data.order.data,
          
         

          arrDate:res.data.arr_date
        })
        // if (res.data.arr_date) {
        //   this.setData({
        //     arrDate:encodeURIComponent(JSON.string(res.data.arr_date)) 
        //   })
          
        // }
      }
      console.log("arrDate",this.data.arrDate)
    }
    console.log("clientList", this.data.clientList);
  },
// 跳转函数
btnclick(e){
 
  let arrDate=encodeURIComponent(JSON.stringify(this.data.arrDate))
  console.log("it",e)
  wx.navigateTo({
    
    　　　　url: `/pages/order/order-detail/detail/detail?arrDate=${arrDate}&orderType=${this.data.orderType}&activeIndex=${this.data.active}&order_id=${e.currentTarget.dataset.id}`
    // 
    　　　})
},
  // 隐藏弹窗
  hidePopup: function () {
    this.setData({
      flag: !this.data.flag
    })
  },
  // 展示弹框
  showPopup() {
    this.userCut(),
      this.setData({
        flag: !this.data.flag
      })
  },


  // 点击按钮时处理逻辑
  handleHidden(e) {

    this.setData({
      isCause: true
    })
  },


  // 退出登录
  handleOutLogin(e) { //   退出登录
    const {
      type
    } = e.currentTarget.dataset
    wx.showModal({
      title: `${type}`,
      content: `是否确认${type}`,
      // 接口调用成功的函数
      success: (result) => {
        if (result.confirm) {
          toast(`${type}成功`, 'success')
          wx.removeStorageSync('token')
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/other/register/register'
            });
          }, 1500)
        } else {
          toast('取消退出登录')
        }
      }
    });
  }
  /*
  
  */
})